﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XnaDevRu.ContentBuilder.Framework;
using System.IO;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

namespace TestEditor
{
    public partial class ImportForm : Form
    {
        private static ContentProject _project = null;

        public enum EImportType
        {
            StaticMesh,
            SkeletalMesh,
            Texture
        };

        public class StaticMeshProps
        {
            bool _convertFromMaxCoords = true;
            public bool ConvertFromMaxCoords
            {
                get { return _convertFromMaxCoords; }
                set { _convertFromMaxCoords = value; }
            }
        };

        public EImportType ImportType;

        public static void InitProject()
        {
            _project = new ContentProject();
            _project.ContentAssemblies.AddAssembly(Directory.GetCurrentDirectory() + "\\ModelImporter.dll");
        }

        public ImportForm(EImportType importType)
        {
            ImportType = importType;
            InitializeComponent();
            _project.RegisterLogger(new TextBoxLogger(buildLog));
        }

        private void locateResource_Click(object sender, EventArgs e)
        {
            _project.ContentFiles.Clear();
            if (ImportType == EImportType.StaticMesh)
            {
                openResourceDialog.Filter = "Any static mesh|*.fbx;*.x;*.obj|fbx|*.fbx|x|*.x|obj|*.obj";
            }else
            if (ImportType == EImportType.Texture)
            {
                openResourceDialog.Filter = "Any texture|*.tga;*.jpg;*.dds|tga|*.tga|jpg|*.jpg|dds|*.dds";
            }
            openResourceDialog.ShowDialog();
            if (openResourceDialog.FileName.Length > 0)
            {
                string selectedFile = openResourceDialog.FileName;
                resourcePath.Text = selectedFile;
                _project.ProjectOptions.RootDirectory = selectedFile.Substring(0, selectedFile.LastIndexOf('\\'));
                _project.ProjectOptions.IntermediateDirectory = _project.ProjectOptions.RootDirectory;
                ContentFile contentFile = _project.InitContentFile(selectedFile);//new ContentFile(selectedFile);
                //string fileExt = selectedFile.Substring(selectedFile.LastIndexOf('.')).ToLower();
                if (ImportType == EImportType.StaticMesh)
                {
                    _project.ProjectOptions.OutputDirectory = Directory.GetCurrentDirectory() + "\\Content\\StaticMeshes\\";
                    contentFile.Processor = "TestXNA3 processor";
                    selectedFileProps.SelectedObject = new StaticMeshProps();
                }
                if (ImportType == EImportType.Texture)
                {
                    _project.ProjectOptions.OutputDirectory = Directory.GetCurrentDirectory() + "\\Content\\Textures\\";
                    contentFile.Processor = "Texture - XNA Framework";
                }
            }
        }

        private void buildProject_Click(object sender, EventArgs e)
        {
            if (_project != null)
            {
                _project.Build(false);
            }
        }
    }
}
